package com.pimee.web.sys;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.pimee.common.util.shiro.ShiroUtils;
import com.pimee.model.SysUnit;
import com.pimee.service.admin.ISysUnitService;
import com.pimee.web.AbstractController;

import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("admin/sys/unit")
public class SysUnitController extends AbstractController {

	private String prefix = "admin/sys/unit";

	@Autowired
	private ISysUnitService sysUnitService;

	@ApiOperation("查询OEM列表")
	@RequiresPermissions("system:unit:list")
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public Object pageList(@RequestParam Map<String, Object> params, ModelMap modelMap) {
		PageInfo<SysUnit> pageList = sysUnitService.pageList(params);
		return setSuccessModelMap(modelMap, pageList);
	}

	@ApiOperation("进入新增OEM页面")
	@GetMapping("/toAdd")
	public String toAdd() {
		return prefix + "/add";
	}

	@ApiOperation("保持新增OEM信息")
	@RequiresPermissions("system:unit:add")
	@PostMapping("/add")
	@ResponseBody
	public Object addSave(@Validated SysUnit param, ModelMap modelMap) {
		String loginName = ShiroUtils.getLoginName();
		param.setCreateBy(loginName);
		param.setUpdateBy(loginName);
		sysUnitService.insertUnit(param);
		return setSuccessModelMap(modelMap);
	}

	@ApiOperation("进入修改OEM页面")
	@GetMapping("/toEdit/{unitId}")
	public String edit(@PathVariable("unitId") Long unitId, ModelMap modelMap) {
		modelMap.put("unit", sysUnitService.selectByKey(unitId));
		return prefix + "/edit";
	}

	@ApiOperation("保持修改OEM信息")
	@RequiresPermissions("system:unit:edit")
	@PostMapping("/edit")
	@ResponseBody
	public Object editSave(@Validated SysUnit param, ModelMap modelMap) {
		param.setUpdateBy(ShiroUtils.getLoginName());
		sysUnitService.updateUnit(param);
		return setSuccessModelMap(modelMap);
	}

	@ApiOperation("删除OEM信息")
	@RequiresPermissions("system:unit:remove")
	@PostMapping("/remove/{unitId}")
	@ResponseBody
	public Object remove(@PathVariable("unitId") Long unitId, ModelMap modelMap) {
		sysUnitService.deleteByKey(unitId);
		return this.setSuccessModelMap(modelMap);
	}
}
